/**
 * <strong>Title : MaterialTypeServiceImpl.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.cm.service.impl<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年9月24日 上午11:25:36<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.cm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.cm.dao.MaterialTypeDao;
import cn.b.sky.mes.cm.model.MaterialType;
import cn.b.sky.mes.cm.model.condition.MaterialTypeCondition;
import cn.b.sky.mes.cm.service.MaterialTypeService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

/**
 * <strong>Title : MaterialTypeServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.cm.service.impl<br></strong>
 * <strong>Description : </strong>@物料类型业务实现@<br> 
 * <strong>Create on : 2015年9月24日 上午11:25:36<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("MaterialTypeService")
public class MaterialTypeServiceImpl implements MaterialTypeService {

  private static final Logger LOGGER = LoggerFactory.getLogger(MaterialTypeServiceImpl.class);

  @Resource
  MaterialTypeDao materialTypeDao;

  @Override
  public List<MaterialType> selectMaterialTypeList(MaterialTypeCondition materialTypeCondition) {
    if (StringUtils.isBlank(materialTypeCondition.getOrderField())) {
      materialTypeCondition.setOrderField("CreateDateTime");
    }
    return materialTypeDao.selectMaterialTypeList(materialTypeCondition);
  }

  @Override
  public int getMaterialTypeCount(MaterialTypeCondition materialTypeCondition) {
    return materialTypeDao.getMaterialTypeCount(materialTypeCondition);
  }

  @Override
  public MaterialType getMaterialType(String id, String siteCode) {
    return materialTypeDao.getMaterialType(id, siteCode);
  }

  @Override
  public void insertMaterialType(MaterialType materialType) {
    materialTypeDao.insertMaterialType(materialType);

  }

  @Override
  public void updateMaterialType(MaterialType materialType) {
    materialTypeDao.updateMaterialType(materialType);

  }

  @Override
  public BatchJobReport deleteMaterialTypes(String materialTypeCode, String siteCode) {
    if (!StringUtils.isBlank(materialTypeCode)) {
      String message = materialTypeCode;
      String cause = "";
      boolean success = true;
      try {
        materialTypeDao.deleteMaterialType(materialTypeCode, siteCode);
      } catch (Exception e) {
        LOGGER.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(errorMsg)) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(),
              "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
    } else {
      return new BatchJobReport("", "id is null", false, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
    }

  }

  /** (非 Javadoc) 
  * Title: getMaterialTypeExactCount
  * Description:
  * @param materialTypeCondition
  * @return
  
  */
  /**
   * <strong>Title : getMaterialTypeExactCount<br></strong>
   * <strong>Description : </strong>@方法注释说明写在此处@<br> 
   * <strong>Create on : 2015年9月24日 下午7:09:28<br></strong>
   * <p>
   * @param materialTypeCondition
   * @return
   * @see cn.b.sky.mes.cm.service.MaterialTypeService#getMaterialTypeExactCount(cn.b.sky.mes.cm.model.condition.MaterialTypeCondition)
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @Override
  public int getMaterialTypeExactCount(MaterialTypeCondition materialTypeCondition) {
    //  Auto-generated method stub
    return materialTypeDao.getMaterialTypeExactCount(materialTypeCondition);
  }

  /** (非 Javadoc) 
  * Title: getMaterialType
  * Description:
  * @param materialTypeCondition
  * @return
  
  */
  /**
   * <strong>Title : getMaterialType<br></strong>
   * <strong>Description : </strong>@方法注释说明写在此处@<br> 
   * <strong>Create on : 2015年9月24日 下午7:16:07<br></strong>
   * <p>
   * @param materialTypeCondition
   * @return
   * @see cn.b.sky.mes.cm.service.MaterialTypeService#getMaterialType(cn.b.sky.mes.cm.model.condition.MaterialTypeCondition)
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @Override
  public MaterialType getMaterialType(MaterialTypeCondition materialTypeCondition) {
    return materialTypeDao.getMaterialType(materialTypeCondition);
  }

  @Override
  public List<MaterialType> selectAllUnLockeMaterialTypes(String siteCode) {
    return materialTypeDao.selectAllUnLockeMaterialTypes(siteCode);

  }

}
